package com.tao.design.decorate

/**
 *
 * @ProjectName:    lab
 * @Package:        com.tao.design.decorate
 * @ClassName:      Beverage
 * @Description:     java类作用描述
 * @Author:         wangxf
 * @CreateDate:     2022/4/24 16:21
 * @UpdateUser:     更新者
 * @UpdateDate:     2022/4/24 16:21
 * @UpdateRemark:   更新说明
 * @Version:        1.0
 */

/**
 * eg：给咖啡加佐料
 *
 * 装饰模式
 *
 */
abstract class Beverage {
    var description = "Unknown beverage"

    fun definBeverage(des: String) {
        description = des
    }

    abstract fun const(): Double;
}

abstract class CondimentBeverage : Beverage() {
    abstract fun addCondiment(des: String)
}

//品种
class Espresso : Beverage() {

    init {
        description = "Espresso"
    }

    override fun const(): Double {
        return 0.8
    }
}
//佐料

class Mocha(val beverage: Beverage) : CondimentBeverage() {
    //附加值计算
    override fun const(): Double {
        return beverage.const() + 1.0
    }

    //添加附加属性
    override fun addCondiment(des: String) {
        beverage.description + ",Mocha"
    }
}

class Sugar(val beverage: Beverage) : CondimentBeverage() {
    //附加值计算
    override fun const(): Double {
        return beverage.const() + 2.0
    }

    //添加附加属性
    override fun addCondiment(des: String) {
        beverage.description + ",Sugar"
    }
}

fun main() {

    //原液
    val espresson = Espresso()

    //附加 +1
    val mocha = Mocha(espresson)
    //附加 +1
    val sugar = Sugar(mocha)

    //计算
    sugar.const();
}
